"""
设备管理相关模型
"""

from typing import Dict, Any, List
from jlpay.model.base import OrgBaseReq, BaseResponse


class UnionPayCodeQueryRequest(OrgBaseReq):
    """银联码查询请求"""
    
    API_URL = "/open/merch/access/device/unionpay/query"
    
    def __init__(self):
        """初始化银联码查询请求"""
        super().__init__()
        # 商户号已在父类OrgBaseReq中定义
    
    def set_merch_no(self, merch_no):
        """
        设置商户号
        
        Args:
            merch_no: 商户号
            
        Returns:
            UnionPayCodeQueryRequest实例
        """
        self.merch_no = merch_no
        return self


class UnionPayCodeQueryResponse(BaseResponse):
    """银联码查询响应"""
    
    def __init__(self, response_dict=None):
        """
        初始化银联码查询响应
        
        Args:
            response_dict: 响应字典
        """
        super().__init__(response_dict)
        self.qrcodes = None  # 二维码列表
        
        if response_dict:
            self.qrcodes = response_dict.get('qrcodes', [])
    
    def get_qrcode_list(self):
        """
        获取二维码列表
        
        Returns:
            二维码列表
        """
        return self.qrcodes or []
    
    def get_first_qrcode(self):
        """
        获取第一个二维码信息
        
        Returns:
            第一个二维码信息字典，如果没有则返回None
        """
        if self.qrcodes and len(self.qrcodes) > 0:
            return self.qrcodes[0]
        return None
    
    def get_qrcode_by_term_no(self, term_no):
        """
        根据终端号获取二维码信息
        
        Args:
            term_no: 终端号
            
        Returns:
            匹配的二维码信息字典，如果没有则返回None
        """
        if self.qrcodes:
            for qrcode in self.qrcodes:
                if qrcode.get('term_no') == term_no:
                    return qrcode
        return None


class UnionPayCodeAddRequest(OrgBaseReq):
    """银联码加机请求"""
    
    API_URL = "/open/merch/access/device/unionpay/add"
    
    def __init__(self):
        """初始化银联码加机请求"""
        super().__init__()
        self.print_merch_name = None
    
    def set_merch_no(self, merch_no):
        """
        设置商户号
        
        Args:
            merch_no: 商户号
            
        Returns:
            UnionPayCodeAddRequest实例
        """
        self.merch_no = merch_no
        return self
    
    def set_print_merch_name(self, print_merch_name):
        """
        设置打印商户名称
        
        Args:
            print_merch_name: 打印商户名称
            
        Returns:
            UnionPayCodeAddRequest实例
        """
        self.print_merch_name = print_merch_name
        return self


class UnionPayCodeAddResponse(BaseResponse):
    """银联码加机响应"""
    
    def __init__(self, response_dict=None):
        """
        初始化银联码加机响应
        
        Args:
            response_dict: 响应字典
        """
        super().__init__(response_dict)
        self.merch_no = None
        self.term_no = None
        self.qrcode_url = None
        
        if response_dict:
            self.merch_no = response_dict.get('merch_no')
            self.term_no = response_dict.get('term_no')
            self.qrcode_url = response_dict.get('qrcode_url')


class DeviceChangeRequest(OrgBaseReq):
    """设备变更请求"""
    
    API_URL = "/open/merch/access/device/change"
    
    def __init__(self):
        """初始化设备变更请求"""
        super().__init__()
        self.term_no = None
        self.new_device_no = None
    
    def set_merch_no(self, merch_no):
        """
        设置商户号
        
        Args:
            merch_no: 商户号
            
        Returns:
            DeviceChangeRequest实例
        """
        self.merch_no = merch_no
        return self
    
    def set_term_no(self, term_no):
        """
        设置终端号
        
        Args:
            term_no: 终端号
            
        Returns:
            DeviceChangeRequest实例
        """
        self.term_no = term_no
        return self
    
    def set_new_device_no(self, new_device_no):
        """
        设置新设备号
        
        Args:
            new_device_no: 新设备号
            
        Returns:
            DeviceChangeRequest实例
        """
        self.new_device_no = new_device_no
        return self


class DeviceChangeResponse(BaseResponse):
    """设备变更响应"""
    
    def __init__(self, response_dict=None):
        """
        初始化设备变更响应
        
        Args:
            response_dict: 响应字典
        """
        super().__init__(response_dict)
        self.merch_no = None
        self.device_no = None
        self.term_no = None
        
        if response_dict:
            self.merch_no = response_dict.get('merch_no')
            self.device_no = response_dict.get('device_no')
            self.term_no = response_dict.get('term_no')


class DeviceCancelRequest(OrgBaseReq):
    """设备撤销请求"""
    
    API_URL = "/open/merch/access/device/cancel"
    
    def __init__(self):
        """初始化设备撤销请求"""
        super().__init__()
        self.term_no = None
    
    def set_merch_no(self, merch_no):
        """
        设置商户号
        
        Args:
            merch_no: 商户号
            
        Returns:
            DeviceCancelRequest实例
        """
        self.merch_no = merch_no
        return self
    
    def set_term_no(self, term_no):
        """
        设置终端号
        
        Args:
            term_no: 终端号
            
        Returns:
            DeviceCancelRequest实例
        """
        self.term_no = term_no
        return self


class DeviceCancelResponse(BaseResponse):
    """设备撤销响应"""
    
    def __init__(self, response_dict=None):
        """
        初始化设备撤销响应
        
        Args:
            response_dict: 响应字典
        """
        super().__init__(response_dict)
        self.device_no = None
        self.term_no = None
        self.merch_no = None
        
        if response_dict:
            self.device_no = response_dict.get('device_no')
            self.term_no = response_dict.get('term_no')
            self.merch_no = response_dict.get('merch_no')


class DeviceAddRequest(OrgBaseReq):
    """设备添加请求"""
    
    API_URL = "/open/merch/access/device/add"
    
    def __init__(self):
        """初始化设备添加请求"""
        super().__init__()
        self.device_type = None
        self.device_no = None
        self.print_merch_name = None
        self.det_address = None
        self.shop_id = None
    
    def set_merch_no(self, merch_no):
        """
        设置商户号
        
        Args:
            merch_no: 商户号
            
        Returns:
            DeviceAddRequest实例
        """
        self.merch_no = merch_no
        return self
    
    def set_device_type(self, device_type):
        """
        设置设备类型
        
        Args:
            device_type: 设备类型
            
        Returns:
            DeviceAddRequest实例
        """
        self.device_type = device_type
        return self
    
    def set_device_no(self, device_no):
        """
        设置设备号
        
        Args:
            device_no: 设备号
            
        Returns:
            DeviceAddRequest实例
        """
        self.device_no = device_no
        return self
    
    def set_print_merch_name(self, print_merch_name):
        """
        设置打印商户名称
        
        Args:
            print_merch_name: 打印商户名称
            
        Returns:
            DeviceAddRequest实例
        """
        self.print_merch_name = print_merch_name
        return self
    
    def set_det_address(self, det_address):
        """
        设置设备地址
        
        Args:
            det_address: 设备地址
            
        Returns:
            DeviceAddRequest实例
        """
        self.det_address = det_address
        return self
    
    def set_shop_id(self, shop_id):
        """
        设置门店ID
        
        Args:
            shop_id: 门店ID
            
        Returns:
            DeviceAddRequest实例
        """
        self.shop_id = shop_id
        return self


class DeviceAddResponse(BaseResponse):
    """设备添加响应"""
    
    def __init__(self, response_dict=None):
        """
        初始化设备添加响应
        
        Args:
            response_dict: 响应字典
        """
        super().__init__(response_dict)
        self.merch_no = None
        self.device_no = None
        self.term_no = None
        
        if response_dict:
            self.merch_no = response_dict.get('merch_no')
            self.device_no = response_dict.get('device_no')
            self.term_no = response_dict.get('term_no') 


class DeviceInfo:
    """设备信息"""
    
    def __init__(self, device_dict=None):
        """
        初始化设备信息
        
        Args:
            device_dict: 设备信息字典
        """
        self.device_no = None  # 设备硬件序列号（SN）
        self.term_no = None  # 终端编号
        self.merch_no = None  # 商户号
        self.status = None  # 状态：0-待启用 1-启用 2-停用 3-注销
        self.model = None  # 设备型号
        self.device_type = None  # 设备类型：02-传统POS 04-智能POS 05-智能POS AD-音响 CD-收银台 EP-电子码牌 PT-打印机 QR-码付设备 SP-立牌
        self.supplier = None  # 供应商
        self.merch_name = None  # 商户名称
        self.print_merch_name = None  # 打印商户名称，签购单上的商户名称
        self.address = None  # 地址
        self.install_time = None  # 加机时间，yyyy-mm-dd HH:mm:ss
        self.update_time = None  # 更新时间，yyyy-mm-dd HH:mm:ss
        
        if device_dict:
            self.device_no = device_dict.get('device_no')
            self.term_no = device_dict.get('term_no')
            self.merch_no = device_dict.get('merch_no')
            self.status = device_dict.get('status')
            self.model = device_dict.get('model')
            self.device_type = device_dict.get('device_type')
            self.supplier = device_dict.get('supplier')
            self.merch_name = device_dict.get('merch_name')
            self.print_merch_name = device_dict.get('print_merch_name')
            self.address = device_dict.get('address')
            self.install_time = device_dict.get('install_time')
            self.update_time = device_dict.get('update_time')
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        return {
            'device_no': self.device_no,
            'term_no': self.term_no,
            'merch_no': self.merch_no,
            'status': self.status,
            'model': self.model,
            'device_type': self.device_type,
            'supplier': self.supplier,
            'merch_name': self.merch_name,
            'print_merch_name': self.print_merch_name,
            'address': self.address,
            'install_time': self.install_time,
            'update_time': self.update_time
        }


class DeviceQueryRequest(OrgBaseReq):
    """终端查询请求"""
    
    API_URL = "/open/merch/access/device/query"
    
    def __init__(self):
        """初始化终端查询请求"""
        super().__init__()
        self.merch_no = None  # 商户号，终端号、机身号不传查该商户下所有的终端信息
        self.term_no = None  # 终端编号，填入终端号时查询该终端号的终端信息，终端号必须为该商户号下的终端
        self.device_no = None  # 设备硬件序列号（SN），填入机身号时查询该设备的终端信息，机身号必须该商户号下加机绑定的设备对应的机身号
    
    def set_merch_no(self, merch_no: str):
        """
        设置商户号
        
        Args:
            merch_no: 商户号
            
        Returns:
            DeviceQueryRequest实例
        """
        self.merch_no = merch_no
        return self
    
    def set_term_no(self, term_no: str):
        """
        设置终端编号
        
        Args:
            term_no: 终端编号
            
        Returns:
            DeviceQueryRequest实例
        """
        self.term_no = term_no
        return self
    
    def set_device_no(self, device_no: str):
        """
        设置设备硬件序列号
        
        Args:
            device_no: 设备硬件序列号
            
        Returns:
            DeviceQueryRequest实例
        """
        self.device_no = device_no
        return self
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        data = super().to_dict()
        data.update({
            'merch_no': self.merch_no,
            'term_no': self.term_no,
            'device_no': self.device_no
        })
        return data


class DeviceQueryResponse(BaseResponse):
    """终端查询响应"""
    
    def __init__(self, response_dict=None):
        """
        初始化终端查询响应
        
        Args:
            response_dict: 响应字典
        """
        super().__init__(response_dict)
        self.devices = None  # 设备列表
        
        if response_dict:
            devices_data = response_dict.get('devices', [])
            if devices_data:
                self.devices = [DeviceInfo(device) for device in devices_data]
    
    def to_dict(self) -> Dict[str, Any]:
        """转换为字典格式"""
        data = super().to_dict()
        data.update({
            'devices': [device.to_dict() for device in self.devices] if self.devices else []
        })
        return data 